<template>
  <van-icon
    @click="onLike"
    color="#777"
    :name="isShow ? 'good-job' : 'good-job-o'"
  />
</template>

<script>
import { addLike, deleteLike } from '@/api/article.js'
export default {
  name: 'aricleLike',
  props: {
    value: {
      type: [Number, String, Object],
      required: true
    },
    articleId: {
      type: [Number, String, Object],
      required: true
    }
  },
  data () {
    return {
      isShow: this.value
    }
  },
  methods: {
    async onLike () {
      try {
        if (this.value) {
          // 已赞，取消点赞
          await deleteLike(this.articleId)
        } else {
          // 没有点赞，添加点赞
          await addLike(this.articleId)
        }
        // 更新视图状态
        this.isShow = !this.isShow
        this.$emit('input', this.value)
      } catch (error) {
        console.log(error)
        this.$toast('操作失败，请重试')
      }
    }
  }
}
</script>
<style lang="less" scoped></style>
